package cn.itcast.item.controller;


import cn.itcast.hmall.dto.common.PageDTO;
import cn.itcast.hmall.dto.common.ResultDTO;
import cn.itcast.hmall.dto.item.SearchItemDTO;
import cn.itcast.hmall.pojo.item.Item;
import cn.itcast.hmall.constants.MqConstants;
import cn.itcast.item.service.ItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private ItemService itemService;
    @Autowired
    private RabbitTemplate rabbitTemplate;


    /**
     * 查询全部商品信息
     *
     * @return
     */
    @PostMapping("all")
    public PageDTO<Item> selectAllPage(@RequestBody Integer i) {
        return itemService.selectAllPage(i);
    }

    /**
     * 分页查询商品
     *
     * @param searchItemDTO
     * @return
     */
    @PostMapping("list")
    public PageDTO<Item> selectPage(@RequestBody SearchItemDTO searchItemDTO) {
        return itemService.selectPage(searchItemDTO);
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Item selectItemById(@RequestParam Long id) {
        return itemService.selectItemById(id);
    }

    /**
     * 新增数据
     *
     * @param item
     * @return
     */
    @PostMapping
    public ResultDTO insertItem(@RequestBody Item item) {
        ResultDTO resultDTO = itemService.insertItem(item);
        rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE,MqConstants.ITEM_INSERT_KEY,item.getId());
        log.info("新增了一条数据：{}",item.getId());
        return resultDTO;
    }

    /**
     * 上架，下架
     *
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/status/{id}/{status}")
    public ResultDTO statusIdItem(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        ResultDTO resultDTO = itemService.statusIdItem(id, status);
        if(status==1){
            rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE,MqConstants.ITEM_UP_KEY,id);
            log.info("上架了一条数据：{}",id);
        }else{
            rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE,MqConstants.ITEM_DOWN_KEY,id);
            log.info("下架了一条数据：{}",id);
        }
        return resultDTO;
    }

    /**
     * 修改商品信息
     *
     * @param item
     * @return
     */
    @PutMapping
    private ResultDTO updateStatus(@RequestBody Item item) {
        ResultDTO resultDTO = itemService.updateStatus(item);
        rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE,MqConstants.ITEM_INSERT_KEY,item.getId());
        log.info("修改了一条数据：{}",item.getId());
        return resultDTO;
    }

    /**
     * 根据id删除商品
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResultDTO deleteById(@PathVariable("id") Long id) {
        ResultDTO resultDTO = itemService.deleteById(id);
        rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE,MqConstants.ITEM_DELETE_KEY,id);
        log.info("删除了一条数据：{}",id);
        return resultDTO;
    }
}
